import express = require("express");
import { DBServer } from "./DBServer";


//Server服务器
export class HttpSer {

    public App = express()
    public port: number = 3000
    public DBServer: DBServer = new DBServer();


    public RunServer() {

        this.App.post('/', this.helloworld.bind(this))

        this.App.all("/login", this.login.bind(this))

        this.App.all("/register", this.addUserInfo.bind(this))

        this.App.all("/remove", this.RemoveUserInfo.bind(this))

        this.App.all("/update", this.UpdateUserInfo.bind(this))

        this.App.all("/user/:naem/:age", this.user.bind(this))

        this.App.use((req, res, next) => {
            console.log("中间件use")
            next();
        });

        this.App.listen(this.port, () => {
            console.log(`服务已开启`)
        })

        //nodejs服务就黑盒子，不能通过路径找到文件 如：需要开放对应的文件目录
        // this.App.use("/public/", express.static("./public/"))

    }

    public helloworld(req, res) {

        let result = this.DBServer.SeleAllData();
        result.then( result => {
            res.send('使用Promise查询' + JSON.stringify(result));
        })
    }

    public login(req, res) {

        let obj = req.query;
        this.DBServer.SeleData(obj.uname, obj.pwd, res);
    }

    /**添加用户信息 */
    public addUserInfo(req, res) {

        let obj = req.query;
        this.DBServer.AddData(obj.name, obj.sex, obj.age, obj.uname, obj.pwd, res);
    }

    /**根据用户名修改用户年龄 */
    public UpdateUserInfo(req, res) {

        let obj = req.query;
        this.DBServer.UpdateData(obj.uname, obj.age, res);
    }

    /**根据用户名删除用户信息 */
    public RemoveUserInfo(req, res) {

        let obj = req.query;
        this.DBServer.RemoveData(obj.uname, res);
    }

    public user(req, res) {
        console.log('123');
    }


}